import { URI,LOGIN, ROUTES } from "@/services/api";
import {request, METHOD, removeAuthorization} from '@/utils/request'

/**
 * 登录服务
 * @param name 账户名
 * @param password 账户密码
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function login(name, password) {  
  return request(LOGIN, METHOD.POST, {
    name: name,
    password: password
  })
}

export async function getRoutesConfig() {
  return request(ROUTES, METHOD.GET)
}

/**
 * 退出登录
 */
export function logout() {
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
  localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
  removeAuthorization()
}


export async function list(search, pageIndex, pageSize) {
  return request(`${URI}/User/GetData`, METHOD.GET, {
    search: search,
    pageIndex: pageIndex,
    pageSize: pageSize
  })
}

export async function save(model) {
  return request(`${URI}/User/Save`, METHOD.POST, {
    model: model,
  });
}
export async function detail(id) {
  return request(`${URI}/User/GetModel`, METHOD.GET, {
    id: id,
  });
}

export async function remove(id) {
  return request(`${URI}/User/Remove?id=`+id, METHOD.POST);
}

export async function editPassword(password, newPassword) {
  return request(`${URI}/User/EditPassword`, METHOD.POST, {
    password: password, 
    newPassword: newPassword
  });
}


export default {
  login,
  logout,
  getRoutesConfig,
  list,
  save,
  detail,
  remove,
  editPassword
}
